What is @project-serum/anchor?
@project-serum/anchor is a framework for building Solana smart contracts. It provides a set of tools and libraries to simplify the development, testing, and deployment of Solana programs. Anchor abstracts away much of the boilerplate code and offers a more developer-friendly experience.
What are @project-serum/anchor's main functionalities?
Program Development
This code demonstrates how to initialize a Solana program using Anchor. It sets up the provider, loads the IDL (Interface Definition Language) for the program, and sends a transaction to initialize the program.
const anchor = require('@project-serum/anchor');
const { SystemProgram } = anchor.web3;
const main = async () => {
const provider = anchor.Provider.env();
anchor.setProvider(provider);
const idl = JSON.parse(require('fs').readFileSync('./target/idl/my_program.json', 'utf8'));
const programId = new anchor.web3.PublicKey('YourProgramPublicKey');
const program = new anchor.Program(idl, programId);
const tx = await program.rpc.initialize({
accounts: {
myAccount: anchor.web3.Keypair.generate().publicKey,
user: provider.wallet.publicKey,
systemProgram: SystemProgram.programId,
},
});
console.log('Transaction signature', tx);
};
main().catch(err => console.error(err));
Testing
This code demonstrates how to write tests for a Solana program using Anchor. It uses Mocha for the test framework and asserts that the account is initialized correctly.
const anchor = require('@project-serum/anchor');
const { SystemProgram } = anchor.web3;
const assert = require('assert');
describe('my_program', () => {
const provider = anchor.Provider.env();
anchor.setProvider(provider);
const program = anchor.workspace.MyProgram;
it('Initializes the account', async () => {
const myAccount = anchor.web3.Keypair.generate();
await program.rpc.initialize({
accounts: {
myAccount: myAccount.publicKey,
user: provider.wallet.publicKey,
systemProgram: SystemProgram.programId,
},
signers: [myAccount],
});
const account = await program.account.myAccount.fetch(myAccount.publicKey);
assert.ok(account.user.equals(provider.wallet.publicKey));
});
});
Client-Side Interaction
This code demonstrates how to interact with a Solana program from the client side using Anchor. It initializes an account and fetches its data.
const anchor = require('@project-serum/anchor');
const main = async () => {
const provider = anchor.Provider.env();
anchor.setProvider(provider);
const idl = JSON.parse(require('fs').readFileSync('./target/idl/my_program.json', 'utf8'));
const programId = new anchor.web3.PublicKey('YourProgramPublicKey');
const program = new anchor.Program(idl, programId);
const myAccount = anchor.web3.Keypair.generate();
await program.rpc.initialize({
accounts: {
myAccount: myAccount.publicKey,
user: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
},
signers: [myAccount],
});
const account = await program.account.myAccount.fetch(myAccount.publicKey);
console.log('Account data:', account);
};
main().catch(err => console.error(err));
Other packages similar to @project-serum/anchor
@solana/spl-token
@solana/spl-token is a library for interacting with the SPL Token program on Solana. It is specialized for token-related operations and does not provide the broader smart contract development features that Anchor offers.
@solana/spl-governance
@solana/spl-governance is a library for building governance systems on Solana. It is focused on governance functionalities and does not offer the general-purpose smart contract development tools that Anchor provides.